<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <div id="app">
    <p>rootCount:{{rootCount}}</p>
    <p>count1:{{count1}}</p>
    <p>doubleCount1:{{doubleCount1}}</p>
    <p>count2:{{count2}}</p>
    <p>doubleCount2:{{doubleCount2}}</p>
    <p>addRootCount:{{addRootCount}}</p>
    <button @click="add">提交add的commit</button>
    <button @click="increment">派发increment的action</button>
    <button @click="incrementByRootCount">派发incrementByRootCount的action</button>
  </div>
  <script src="vue.js"></script>
  <script src="my-vuex.js"></script>
  <script>
    //Vue中使用Vuex插件，此插件的作用是给Vue实例添加$store属性
    Vue.use(Vuex);
    let counter1 = {
      namespaced:true,
      state: { count: 1 },
      mutations:{
        add(state,payload,rootState){
          payload = typeof payload === 'number'?payload:1;
          console.log('counter1 rootState',rootState)
          state.count+=payload;
        }
      },
      getters: {
        //在模块内的getters中state指的是模块内的局部状态
        //对于模块内部的 getter，根节点状态会作为第三个参数暴露出来
        doubleCount1(state, getters, rootState,rootGetters) {
          console.log('state',state)
          console.log('getters',getters)
          console.log('rootState',rootState)
          console.log('rootGetters',rootGetters)
          return state.count * 2;
        },
        addRootCount(state, getters, rootState){
          return state.count+rootState.count;
        }
      },
      actions:{//dispatch('counter1/increment')
        increment({commit,state,rootState}){
          //unknown local mutation type: counter1/add, global type: counter1/counter1/add
          //在想在这里修改全局命名空间下的count,可以加一个参数
          commit('add',undefined,{root:false});
        },
        incrementByRootCount({commit,state,rootState}){
          //在提交add变更的时候，payload传递的是全局命名空间下的count
          commit('add',rootState.count);
        },
        // 如果想在一个namespaced:true的模块内注册一个有响应全局动作的action
       /*  globalAction(){// store.dispatch(`counter1/globalAction`);
          console.log('globalAction')
        }, */
        globalAction:{
          root: true,//想把这个action注册到全局
          handler(namespacedContext, payload){
            console.log('globalAction',namespacedContext)
          }
        }
      }
    }
    let counter2 = {
      namespaced:true,
      state: { count: 2 },
      mutations:{
        add(state,payload,rootState){
          console.log('rootState',rootState)
          state.count++;
        }
      },
      getters: {
        doubleCount2(state, getters, rootState) {
          return state.count * 2;
        }
      },
      actions:{
        increment({commit,state,rootState}){
          commit('add');
        }
      }
    }
    const store = new Vuex.Store({
      state: { count: 10 },
      mutations:{
        add(state,payload){
          state.count++;
        }
      },
      actions:{
        increment({commit}){
          commit('add');
        }
      },
      modules: {
        counter1,
        counter2
      }
    })
    const { mapState:mapState1,mapGetters:mapGetters1, mapMutations:mapMutations1,mapActions:mapActions1 } = Vuex.createNamespacedHelpers('counter1');
    const { mapState:mapState2,mapGetters:mapGetters2 } = Vuex.createNamespacedHelpers('counter2');
    var vm = new Vue({
      el: '#app',
      store,
      computed: {
        ...Vuex.mapState({
          rootCount:state=>state.count,
        }),
        ...mapState1({
          count1:state=>state.count,
        }),
        ...mapState2({
          count2:state=>state.count
        }),
        //把getters映射为组件的计算属性
        ...mapGetters1([
          'doubleCount1',
          `addRootCount`
        ]),
        ...mapGetters2([
          `doubleCount2`
        ])
      },
      methods:{
        //把mutations映射为commit方法
        ...mapMutations1(['add']),
        //把action映射为 dispatch方法
        ...mapActions1(['increment','incrementByRootCount']),
      }
    })

  </script>
</body>

</html>